<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>发表新帖</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="keywords" content="海角论坛">
    <meta name="description" content="海角论坛">
    <link rel="stylesheet" href="../../static/res/layui/css/layui.css">
    <link rel="stylesheet" href="../../static/res/css/global.css">
</head>
<body>

<div class="fly-header layui-bg-black">
    <div class="layui-container">
        <a class="fly-logo" href="/">
            <img src="../../static/res/images/logo.png" alt="layui">
        </a>


        <ul class="layui-nav fly-nav-user" id="ul_user">

            <!-- 未登入的状态 -->

            <li class="layui-nav-item">
                <a class="iconfont icon-touxiang layui-hide-xs" href="/login"></a>
            </li>
            <li class="layui-nav-item">
                <a href="/login">登录</a>
            </li>
            <li class="layui-nav-item">
                <a href="/reg">注册</a>
            </li>

            <!-- 登入后的状态 -->

            <!--
            <li class="layui-nav-item">
              <a class="fly-nav-avatar" href="javascript:;">
                <cite class="layui-hide-xs">贤心</cite>
                <i class="iconfont icon-renzheng layui-hide-xs" title="认证信息：layui 作者"></i>
                <i class="layui-badge fly-badge-vip layui-hide-xs">VIP3</i>
                <img src="https://tva1.sinaimg.cn/crop.0.0.118.118.180/5db11ff4gw1e77d3nqrv8j203b03cweg.jpg">
              </a>
              <dl class="layui-nav-child">
                <dd><a href="../user/set.html"><i class="layui-icon">&#xe620;</i>基本设置</a></dd>
                <dd><a href="../user/message.html"><i class="iconfont icon-tongzhi" style="top: 4px;"></i>我的消息</a></dd>
                <dd><a href="../user/home.html"><i class="layui-icon" style="margin-left: 2px; font-size: 22px;">&#xe68e;</i>我的主页</a></dd>
                <hr style="margin: 5px 0;">
                <dd><a href="" style="text-align: center;">退出</a></dd>
              </dl>
            </li>
            -->
        </ul>
    </div>
</div>

<div class="layui-container fly-marginTop">
    <div class="fly-panel" pad20 style="padding-top: 5px;">
        <!--<div class="fly-none">没有权限</div>-->
        <div class="layui-form layui-form-pane">
            <div class="layui-tab layui-tab-brief" lay-filter="user">
                <ul class="layui-tab-title">
                    <li class="layui-this">发表新帖<!-- 编辑帖子 --></li>
                </ul>
                <div class="layui-form layui-tab-content" id="LAY_ucm" style="padding: 20px 0;">
                    <div class="layui-tab-item layui-show">
                        <form>
                            <div class="layui-row layui-col-space15 layui-form-item">
                                <div class="layui-col-md3">
                                    <label class="layui-form-label">所在专栏</label>
                                    <div class="layui-input-block">
                                        <label for="type">
                                            <select id="type" name="class">
                                                <option value="-1"></option>
                                                <option value="1">提问</option>
                                                <option value="2">分享</option>
                                                <option value="3">讨论</option>
                                            </select>
                                        </label>
                                    </div>
                                </div>
                                <div class="layui-col-md9">
                                    <label for="L_title" class="layui-form-label">标题</label>
                                    <div class="layui-input-block">
                                        <input type="text" id="L_title" name="title"
                                               autocomplete="off" class="layui-input">
                                        <!-- <input type="hidden" name="id" value=""> -->
                                    </div>
                                </div>
                            </div>

                            <div class="layui-form-item layui-form-text">
                                <div class="layui-input-block">
                                    <textarea id="L_content" name="content"
                                              placeholder="详细描述" class="layui-textarea fly-editor"
                                              style="height: 260px;"></textarea>
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <div class="layui-inline">
                                    <label class="layui-form-label">悬赏飞吻</label>
                                    <div class="layui-input-inline" style="width: 190px;">

                                        <select id="reward" name="reward"class="layui-input">
                                          <option value="10">10</option>
                                          <option value="20">20</option>
                                          <option value="30">30</option>
                                          <option value="50">50</option>
                                          <option value="100">100</option>
                                           <option value="200">200</option>
                                           <option value="500">500</option>
                                        </select>
                                    </div>
                                    <div class="layui-form-mid layui-word-aux">发表后无法更改飞吻</div>
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <label for="L_vercode" class="layui-form-label">人类验证</label>
                                <div class="layui-input-inline">
                                    <input type="text" id="L_vercode" name="vercode"
                                           placeholder="请输入验证码" autocomplete="off" class="layui-input">
                                </div>
                                <div class="layui-form-mid">
<!--?                                    <span style="color: #c00;" id="verifyCode"></span>-->
                  <img src="/image_code?1001" id="img" class="get_pic_code" onclick="generateImageCode()">


                                </div>
                            </div>
                        </form>
                        <div class="layui-form-item">
                            <button class="layui-btn" id="subbtn">立即发布</button>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<div class="fly-footer">
    <p><a href="#">海角论坛</a> 2019 &copy; <a href="#">葫芦娃出品</a></p>

</div>
<script src="../../static/json/jquery-3.3.1.min.js"></script>
<script src="../../static/res/layui/layui.js"></script>
<script src="../../static/js/jquery-3.3.1.js"></script>
<script>
    function generateImageCode() {
       var now = new Date();
    // 通过a标签的ID获得一个a标签
    var a = document.getElementById("img");
    // 修改a的href属性
    a.src = "/image_code?" + now.getTime();
    }

    layui.cache.page = 'jie';
    layui.cache.user = {
        username: '游客'
        , uid: -1
        , avatar: '../../static/res/images/avatar/00.jpg'
        , experience: 83
        , sex: '男'
    };

    layui.config({
        version: "3.0.0"
        , base: '../../static/res/mods/'
    }).extend({
        fly: 'index'
    }).use('fly');

    layui.use('layer');

    $(function () {

        getUser();

        getVerifyCode();

    });

    function getUser() {
        $.ajax({
            type: "get",
            url:  "/getUser",
            dataType: "json",
            headers: createAuthorizationTokenHeader(),
            success: function (data) {
                console.log(data);
                if (data) {
                    let appendVal = `
                        <li class="layui-nav-item">
                          <a class="fly-nav-avatar" href="javascript:;">
                            <cite class="layui-hide-xs">${data.username}</cite>
                            <i class="iconfont icon-renzheng layui-hide-xs" title="认证信息：layui 作者"></i>
                            <i class="layui-badge fly-badge-vip layui-hide-xs">VIP${data.level}</i>
                            <img src="${data.picture}">
                          </a>
                          <dl class="layui-nav-child">
                            <dd><a href="../user/set.html"><i class="layui-icon">&#xe620;</i>基本设置</a></dd>
                            <dd><a href="../user/message.html"><i class="iconfont icon-tongzhi" style="top: 4px;"></i>我的消息</a></dd>
                            <dd><a href="../user/home.html"><i class="layui-icon" style="margin-left: 2px; font-size: 22px;">&#xe68e;</i>我的主页</a></dd>
                            <hr style="margin: 5px 0;">
                            <dd><a href="javascript:logout();" style="text-align: center;">退出</a></dd>
                          </dl>
                        </li>
                    `;

                    $("#ul_user").html(appendVal);
                }
            },
            error: function (data) {
                console.log(data);
                layer.alert("请先登录", function () {
                    location.href = "/login";
                })
            }
        })
    }

    function getVerifyCode() {
        $.ajax({
            type: "get",
            url: "getVerifyCode",
            dataType: "json",

            success: function (data) {
                console.log(data);
                if (data) {
                    $("#verifyCode").text(data.verifCode);
                    localStorage.setItem("answer", data.codes);
                }
            }
        })
    }

    $("#subbtn").on('click', function (e) {

        let type = $("#type").val();
        let title = $("#L_title").val();
        let content = $("#L_content").val();
        let reward = $("#reward").val();
        let img_code = $("#L_vercode").val();

        var params = {
            "type": type,
            "title": title,
            "content": content,
            "reward": reward,
            "img_code": img_code
        };
        if (type == -1) {
            layer.msg("请选择专栏");
            e.preventDefault();
            return;
        }

        if (!title) {
            layer.msg("标题不能为空");
            e.preventDefault();
            return;
        }

        if (!content) {
            layer.msg("内容不能为空");
            e.preventDefault();
            return;
        }
        if (!img_code) {
            layer.msg("验证码不能为空");
            e.preventDefault();
            return;
        }
      $.ajax({
            url: "/user/add",
            type: "post",
            data: JSON.stringify(params),
            contentType: "application/json",
            success: function (resp) {
                if (resp.errno == "0") {
                   location.href="/"
                }
                else if (resp.errno == "8009"){
                    alert(resp.msg)
                    location.href="/login"
                }
                else if (resp.errno == "9006"){
                    alert(resp.msg)
                    location.href="/user/index"
                }
                else {
                    alert(resp.msg)
                    generateImageCode()
                }
    }})
    });


</script>

</body>
</html>